// javascripts/dynamic_categories.js.erb
var subcategories = new Array();
<% for subcategory in @subcategories -%>
  categories.push(new Array(<%= subcategory.category_id %>, '<%=h subcategory.subcategory_name %>', <%= subcategory.id %>));
<% end -%>

function categorySelected() {
  category_id = $('item_category_id').getValue();
  options = $('item_subcategory_id').options;
  options.length = 1;
  subcategory.each(function(subcategory) {
    if (subcategory[0] == category_id) {
      options[options.length] = new Option(subcategory[1], subcategory[2]);
    }
  });
  if (options.length == 1) {
    $('subcategory_field').hide();
  } else {
    $('subcategory_field').show();
  }
}

document.observe('dom:loaded', function() {
  categorySelected();
  $('item_category_id').observe('change', categorySelected);
}